<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>登录服务器页面</title>
    <script src="https://www.suzi-info.com/cdn/crypto-js.min.js" ></script>
    <script>
        // 与后端保持一致的 key 和 iv...
        let KEY = "";
        let IV = "";

        // 手动填充函数
        function padData(data) {
            const blockSize = 16;
            const paddingLength = blockSize - (data.length % blockSize);
            const padding = '\0'.repeat(paddingLength);
            return data + padding;
        }

        // 加密函数
        function encryptData(data) {
            // 手动填充数据
            const paddedData = padData(data);
            const keyHex = CryptoJS.enc.Utf8.parse(KEY);
            const ivHex = CryptoJS.enc.Utf8.parse(IV);
            const encrypted = CryptoJS.AES.encrypt(paddedData, keyHex, {
                iv: ivHex,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.NoPadding
            });
            return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
        }

        // 解密函数
        function decryptData(ciphertext) {
            const keyHex = CryptoJS.enc.Utf8.parse(KEY);
            const ivHex = CryptoJS.enc.Utf8.parse(IV);
            const ciphertextHex = CryptoJS.enc.Base64.parse(ciphertext);
            const encrypted = CryptoJS.lib.CipherParams.create({ ciphertext: ciphertextHex });
            const decrypted = CryptoJS.AES.decrypt(encrypted, keyHex, {
                iv: ivHex,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.NoPadding
            });
            let decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
            // 过滤掉填充字符 \0
            return decryptedStr.replace(/\0/g, '').trim();
        }
        // 清除所有 Cookie
        function clearAllCookies() {
            const cookies = document.cookie.split(";");
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i];
                const eqPos = cookie.indexOf("=");
                const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
                document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
            }
        }
        // 设置 Cookie
        function setCookie(name, value) {
            const date = new Date();
            date.setFullYear(date.getFullYear() + 100);
            const expires = "; expires=" + date.toUTCString();
            document.cookie = name + "=" + (value || "") + expires + "; path=/";
        }

        // 获取 Cookie
        function getCookie(name) {
            const nameEQ = name + "=";
            const ca = document.cookie.split(';');
            for (let i = 0; i < ca.length; i++) {
                let c = ca[i];
                while (c.charAt(0) === ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        // 页面加载时检查 Cookie 并填充表单
        window.onload = function () {
            // 发起一个同步的 XMLHttpRequest 请求，获取当前页面的响应
            const xhr = new XMLHttpRequest();
            xhr.open('GET', window.location.href, false);
            xhr.send();
            // 从响应头中获取 AES 加密所需的 key 和 iv
            KEY = xhr.getResponseHeader('X-AES-Key');
            IV = xhr.getResponseHeader('X-AES-IV');


            const username = getCookie('username');
            const encryptedPassword = getCookie('password');
            if (username && encryptedPassword) {
                document.getElementById('username').value = username;
                const password = decryptData(encryptedPassword);
                document.getElementById('password').value = password;
            }
        };

        // 表单提交时保存用户名和密码到 Cookie
        function saveToCookie() {
            // clearAllCookies();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const encryptedPassword = encryptData(password);
            setCookie('username', username);
            setCookie('password', encryptedPassword);
            // 将加密后的密码赋值给表单中的密码输入框
            document.getElementById('password').value = encryptedPassword;
            return true;
        }
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: #fff;
            padding: 20px 30px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 320px;
        }
        .logo-title-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
        }
        .login-container .logo {
            width: 60px;
            margin-right: 10px;
        }
        .login-container .title {
            font-size: 20px;
            font-weight: bold;
        }
        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .login-container input[type="text"],
        .login-container input[type="password"] {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        .login-container form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .login-container button {
            width: calc(100% - 20px);
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .login-container button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="logo-title-wrapper">
        <img class="logo" src="/login/logo.png" alt="Logo">
        <div class="title">MES 云平台大数据</div>
    </div>
    <h2>登录</h2>
    <form action="/login/login-submit" method="post" onsubmit="return saveToCookie()" autocomplete="off">
        <input type="text" id="username" name="username" placeholder="用户名" required autocomplete="off">
        <input type="password" id="password" name="password" placeholder="密码" required autocomplete="off">
        <!-- 填充 tenantId 并隐藏输入框 -->
        <input type="text" id="tenant" name="tenant" value="${tenantId}" style="display: none;">
        <button type="submit">登录</button>
    </form>
</div>
</body>
</html>